<template>
  <!-- <div>sub-Category</div> -->
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @filter-change="filterChange" />
      <!-- 全选 -->
      <xtx-checkbox v-model="isAllChecked">全选</xtx-checkbox>{{isAllChecked}}
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序选项 -->
        <SubSort @sort-change="sortChange" />
        <!-- 列表 -->
        <ul>
          <li v-for="item in goodsList" :key="item.id">
            <GoodsItem :goods="item" />
          </li>
        </ul>
        <!-- 加载组件 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData"></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import SubBread from './components/SubBread.vue'
import SubFilter from './components/SubFilter.vue'
import SubSort from './components/SubSort'
import GoodsItem from './components/GoodsItem'
import XtxInfiniteLoading from '@/components/XtxInfiniteLoading'
import { findSubCategoryGoods } from '@/api/category'
export default {
  name: 'category-sub-page',
  components: {
    SubBread, SubFilter, SubSort, GoodsItem, XtxInfiniteLoading
  },
  setup() {
    // 1. 基础布局
    // 2. 无限加载组件
    // 3. 动态加载数据且渲染
    // 4. 任何筛选条件变化需要更新列表
    const route = useRoute()
    const loading = ref(false)
    const finished = ref(false)
    const goodsList = ref([])
    const isAllChecked = ref(true)
    // 查询参数
    let reqParams = {
      page: 1,
      pageSize: 20
    }
    const getData = () => {
      loading.value = true
      reqParams.categoryId = route.params.id
      console.log('此处请求数据>>>>>>')
      findSubCategoryGoods(reqParams).then(({ result }) => {
        if (result.items.length) {
          goodsList.value.push(...result.items)
          reqParams.page++
        } else {
          // 加载完毕
          finished.value = true
        }
        // 请求结束
        loading.value = false
      })
    }
    // 切换二级分类重新加载
    watch(() => route.params.id, (newVal) => {
      if (newVal && route.path === ('/category/sub/' + newVal)) {
        //finished改变会自动触发getData
        finished.value = false
        // 当产品列表为空的时候，加载组件进入视图，自动调接口
        goodsList.value = []
        reqParams = {
          page: 1,
          pageSize: 20
        }
      }
    })
    // 1.更改排序组件的筛选数据，重新请求
    const sortChange = (sortParams) => {
      console.log('sortParams>>>>>>', sortParams)
      finished.value = false
      // 合并参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodsList.value = []
    }
    // 2.更改筛选组件的数据，重新请求
    const filterChange = (filterParams) => {
      console.log('filterParams>>>>>>', filterParams)
      finished.value = false
      // 合并参数
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []
    }
    return {
      isAllChecked,
      finished,
      loading,
      getData,
      goodsList,
      sortChange,
      filterChange
    }
  }
}
</script>
<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>